$().ready(function () {
    validateRule();
});
$.validator.setDefaults({
    submitHandler: function () {
        save();
    }
});

$('input[type=radio][name=usableFlag]').change(function () {
    if (this.value == 1) {
        $("#selectTeam").show();
        $("#isJuhepayTicket1").removeAttr("checked");
        $("#isJuhepayTicket0").prop("checked",true);
        $("#isJuhepayTicket0").prop("disabled", true);
        $("#isJuhepayTicket1").prop("disabled", true);
    } else {
        $("#isJuhepayTicket0").prop("disabled", false);
        $("#isJuhepayTicket1").prop("disabled", false);
        $("#selectTeam").hide();
        $('#usableTeam').val("");
        $('#memberNames').val("");
    }
});

function save() {
    /*	if( $("#rangeType").val()==1 && $("#ticketImg").val()==""){
            parent.layer.msg("请上传图片");
            return;
        }*/
    var type = $('#type').val();//选中的值
    var rangeType = $('#rangeType').val();//选中的值
    var cardNumber = $('#cardNumber').val();//选中的值
    var password = $('#password').val();//选中的值
    var usableTeam = $('#usableTeam').val();//选中的值
    if (type == 4) {
        if (rangeType != 2 && rangeType != 3) {
            layer.alert("单品抵扣券必须指定商品!", {icon: 5});
            return false;
        }
    } else if (type == 5) {
        if (!cardNumber || !password) {
            layer.alert("卡号和密码不能为空!", {icon: 5});
            return false;
        }
    }

    var usableFlag = $("input[name='usableFlag']:checked").val();
    if (usableFlag == 1) {
        if (!usableTeam) {
            layer.alert("请选择可使用门店!", {icon: 5});
            return false;
        }
    }

    $.ajax({
        cache: true,
        type: "POST",
        url: "/promotion/ticket/save",
        data: $('#signupForm').serialize(),// 你的formid
        async: false,
        error: function (request) {
            parent.layer.alert("Connection error");
        },
        success: function (data) {
            if (data.code == 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);
            } else {
                if (data.msg) {
                    parent.layer.alert(data.msg)
                }
            }

        }
    });
}


function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            ticketName: {
                required: true
            },
            teamId: {
                required: true
            },
            type: {
                required: true
            },
            total: {
                required: true
            },
            count: {
                required: true
            },
            amount: {
                required: true
            },
            condition: {
                required: true,
                // digits: true
            }
        },
        messages: {
            ticketName: {
                required: icon + "优惠券标题不能为空"
            },
            teamId: {
                required: icon + "所属门店不能为空"
            },
            type: {
                required: icon + "优惠券类型不能为空"
            },
            total: {
                required: icon + "优惠券发放总数不能为空"
            },
            count: {
                required: icon + "单张券可使用次数"
            },
            amount: {
                required: icon + "优惠券面额不能为空"
            },
            condition: {
                required: icon + "优惠券使用条件不能为空",
                //digits: icon + "使用条件必须为整数"
            }
        }
    })
}

/**
 * 设置选择的商品信息
 * @param productId
 * @param title
 */
function loadProductInfo(productId, title) {
    $('#productId').val(productId);
    $('#productName').val(title);
}

/**
 * 商品选择点击
 */
function selectProduct() {
    var centreId = $("*[name='centreId']").val();
    var shopId = $("#teamId option:selected").val();
    if (!shopId) {
        shopId = $("#shopId").val();
    }
    layer.open({
        id: "ticket_limit_product",
        type: 2,
        title: "绑定商品",
        maxmin: true,
        shadeClose: true,
        area: ['70%', '80%'],
        content: "/promotion/ticket/limitProduct?centreId=" + centreId + "&teamId=" + shopId
    })
}

function onThumbBlur() {
    var url = $.trim($('#thumb').val());
    if (url) {
        if (url.indexOf("http") >= 0 || url.indexOf("https") >= 0) {
            $('#categoryAddthumbImg').attr('src', url);
        } else {
            var prefix = "http://pic.jinzhishenghuo.com";
            $('#categoryAddthumbImg').attr('src', prefix + url);
        }
    }
}

function loadImgsInfo(domId, result) {
    if (!result || result.length < 1) {
        parent.layer.msg("上传图片失败!", {icon: 7});
        return;
    }
    $('#' + domId).val(result.join(","));
    /*var thumbUrl = result.split(",");
    for (var i = 0;i<thumbUrl.length;i++) {
    	var html = '<img class="img-responsive" id="categoryAddthumbImg" src="'+thumbUrl[i]+'" style="max-width: 30%">';
        $('#imgDiv').append(html);
    }*/

}

//批量上传图片
function selectImgs(domId) {
    layer.open({
        id: 'selectImgs',
        type: 2,
        title: '批量选择图片',
        maxmin: true,
        shadeClose: false,
        area: ['90%', '90%'],
        content: '/platform/config/batchUploadImg/' + domId // iframe的url
    });
}


/**
 * 分类选择变更
 * @param category
 */
function categoryChange(category) {
    var categoryId = category.value;
    if (categoryId) {
        $.ajax({
            url: "/category/manage/ccates",
            type: "POST",
            data: {
                parentId: categoryId
            },
            dataType: 'JSON',
            error: function (request) {
                parent.layer.alert("Connection error");
            },
            success: function (data) {
                if (data.code == 0) {
                    var ccates = data.ccates;
                    if (ccates) {
                        $('#ccate').empty();
                        $('#ccate').append("<option value>" + "二级分类" + "</option>");
                        for (var i = 0; i < ccates.length; i++) {
                            var item = ccates[i];
                            $('#ccate').append("<option value='" + item.id + "'>" + item.name + "</option>");
                        }
                    }
                }
            }
        })
    }
}

/**
 * 行业品牌选择变更
 * @param category
 */
function industryChange(industry) {

    var industryId = industry.value;
    if (industryId) {
        $.ajax({
            url: "/centre/industryBrand/getIndustryBrandListByIndustryId",
            type: "POST",
            data: {
                parentId: industryId
            },
            dataType: 'JSON',
            error: function (request) {
                parent.layer.alert("Connection error");
            },
            success: function (data) {

                if (data.code == 0) {

                    var industryBrandList = data.industryBrandList;
                    if (industryBrandList) {
                        $('#industryBrandId').empty();
                        $('#industryBrandId').append("<option value>" + "行业品牌" + "</option>");
                        for (var i = 0; i < industryBrandList.length; i++) {

                            var item = industryBrandList[i];
                            $('#industryBrandId').append("<option value='" + item.id + "'>" + item.name + "</option>");
                        }
                    }
                }
            }
        })
    }
}

function onTypeChange() {
    var type = $("#type").val();
    if (type == 5) {
        $("#camilo").show();
        $("#isJuhepayTicket0").prop("checked","checked");
        $('input:radio[name="isJuhepayTicket"]').prop("disabled",true);
    } else if(type == 3){
        $("#isJuhepayTicket0").prop("checked","checked");
        $('input:radio[name="isJuhepayTicket"]').prop("disabled",true);
    }else {
        $('input:radio[name="isJuhepayTicket"]').prop("disabled",false);
        $("#camilo").hide();
        $("#cardNumber").val("");
        $("#password").val("");
    }
}

/**
 * 门店选择
 */
function selectTeam() {
    let ids = $('#usableTeam').val();
    layer.open({
        id: "ticket_select_team",
        type: 2,
        title: "选择可使用门店",
        maxmin: true,
        shadeClose: true,
        area: ['70%', '80%'],
        content: "/promotion/ticket/selectTeam?ids = " + ids
    })
}

/**
 * 设置会员信息
 * @param memberId
 * @param memberName
 */
function loadTeamInfo(memberId, memberName) {
    $('#usableTeam').val(memberId);
    $('#memberNames').val(memberName);
}